package com.centronix.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.centronix.bean.ComboBean;
import com.centronix.dao.ProductTypeDAO;
import com.centronix.domain.ProductType;
import com.centronix.manager.ProductTypeManager;

@Service("productTypeManager")
@Transactional
public class ProductTypeManagerImpl implements ProductTypeManager{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ProductTypeDAO productTypeDAO;
	
	public List<ProductType> findAllProductType(){
		return productTypeDAO.findAll();
	}
	
	public Boolean saveProductType(ProductType productType){
		productTypeDAO.saveAndFlush(productType);
		return true;
	}
	
	public Boolean deleteProductType(int typeId){
		productTypeDAO.delete(typeId);
		return true;
	}
	
	public List<ComboBean> getProductTypeCombo(int catId){
		List<ComboBean> comboBeans = new ArrayList<>();
		comboBeans.add(ComboBean.getDefaultCombo());
		List<ProductType> productTypes = null;
		if(catId == -1){
			productTypes= productTypeDAO.findAll();
		}else{
			productTypes= productTypeDAO.findByProductCategoryId(catId);
		}
		
		ComboBean comboBean = null;
		for (ProductType productType : productTypes) {
			comboBean = new ComboBean();
			copyProductTypeToComboBean(productType, comboBean);
			comboBeans.add(comboBean);
		}
		return comboBeans;
	}
	
	public void copyProductTypeToComboBean(final ProductType productType, final ComboBean comboBean) {
		comboBean.setValue(String.valueOf(productType.getTypeId()));
		comboBean.setLabel(productType.getNarrative());
	}

	public ProductTypeDAO getProductTypeDAO() {
		return productTypeDAO;
	}

	public void setProductTypeDAO(ProductTypeDAO productTypeDAO) {
		this.productTypeDAO = productTypeDAO;
	}
}
